/*
*存放通用的接口
*/
import http from '@/common/http'
import util from '@/common/util'
import store from '@/store'
export default {
	USER_KEY: "USER_KEY",
	/**
	 * 检查是否的呢轮毂。未登录跳转到登录页面
	 * @param {Object} backpage
	 * @param {Object} backtype
	 */
	checkLogin(backpage, backtype) {
		var user = uni.getStorageSync(this.USER_KEY);
		if (!user) {
			util.goTo('/pages/login/login')
			return false;
		}
		//store.commit('base/login', user); //保存到vuex
		this.getuserinfo()//初始用户信息
		return user;
	},
	/**
	 * 判断是否登录。登录返回用户信息，未登录返回null
	 */
	isLogin() {
		var user = uni.getStorageSync(this.USER_KEY);		
		return user;
	},
	/**
	 * 账号密码登录，json格式提交.。登录成功跳转到首页store.state.pages.home.path
	 * @param {Object} user {username: user.username,password: user.password,type: "user"}
	 */
	login(user) {
		var that = this;
		http.post("/api/login", JSON.stringify({
			username: user.username,
			password: user.password,
			type: "user"
				//#ifdef APP-PLUS	
				,
			imei: plus.device.imei,
			platform: plus.os.name
			//#endif
		})).then(res => {
			if (res.status) {
				store.commit('base/login', res.data); //保存到vuex
				uni.setStorage({ //缓存用户登陆状态
					key: that.USER_KEY,
					data: res.data
				})
				if (store.state.base.loginInfo.forcedLogin) { //强制登录时使用reLaunch方式跳转过来 返回首页也使用reLaunch方式
					uni.reLaunch({
						url: store.state.pages.home.path,
					});
				} else {
					util.back();
				}
			} else {
				util.msg("登录失败,用户名或密码错误")
			}
		}).catch(e => util.msg("登录失败:" + e))
	},
	/**
	 * 微信小程序登录。表单格式提交。登录成功跳转到首页store.state.pages.home.path
	 * @param {Object} user {code: user.code,username: user.username,avatar:user.avatar,type: "user"	}
	 */
	wxMpLogin(user) {
		var that = this;
	 return	http.postForm("/api/snslogin", {
			code: user.code,
			username: user.username,
			avatar: user.avatar,
			type: "user"
		}).then(res => {
			if (res.status) {
				store.commit('base/login', res.data); //保存到vuex
				uni.setStorage({ //缓存用户登陆状态
					key: that.USER_KEY,
					data: res.data
				})
				if (store.state.base.loginInfo.forcedLogin) { //强制登录时使用reLaunch方式跳转过来 返回首页也使用reLaunch方式
					uni.reLaunch({
						url: store.state.pages.home.path,
					});
				} else {
					util.back();
				}
			} else {
				util.msg("登录失败")
			}
		}).catch(e => util.msg("登录失败:" + e))
	},
	/**
	 * 微信APP登录。表单格式提交。登录成功跳转到首页store.state.pages.home.path
	 * @param {Object} user {code: user.code,type: "user"	}
	 */
	wxAppLogin(user) {
		var that = this;
	  return http.postForm("/api/wxapplogin", {
			code: user.code,
			access_token: user.access_token,
			openid: user.openid,
			type: "user"
		}).then(res => {
			if (res.status) {
				store.commit('base/login', res.data); //保存到vuex
				uni.setStorage({ //缓存用户登陆状态
					key: that.USER_KEY,
					data: res.data
				})
				if (store.state.base.loginInfo.forcedLogin) { //强制登录时使用reLaunch方式跳转过来 返回首页也使用reLaunch方式
					uni.reLaunch({
						url: store.state.pages.home.path,
					});
				} else {
					util.back();
				}
			} else {
				util.msg("登录失败"+res.msg)
			}
		}).catch(e => util.msg("登录失败:" + e))
	},
	reg(user) {
		/* uni.request({
			url: that.app.apiHost + "?m=login&a=logout&ajax=1",
			data: {
				"authcode": that.app.getAuthCode(),
				"fromapp": that.app.fromapp()
			},
			success: function(res) {
				that.$store.commit('login', res); //保存到vuex
				uni.setStorage({ //缓存用户登陆状态
					key: USERS_KEY,
					data: res
				})
				that.$util.goTo(this.$store.state.pages.login) //跳转到个人中心页面
			},
			error: function() {
				uni.showToast({
					title: '注册失败'
				});
			}
		}) */
		uni.showToast({
			icon: 'none',
			title: '注册失败'
		});
	},
	/**
	 * @param {Object} callback 回调函数
	 * @param {Object} scope 回调函数的作用域，默认为null表示api对象
	 */
	getuserinfo(callback,scope) {
		var that = this;
		var user = this.isLogin()
		user && http.get("/api/getuserinfo").then(res => {
			store.commit('base/login', res.data); //保存到vuex			
			if (callback) {
				//callback(res)
				callback.apply(scope||this,[res]);
			}
		}).catch(res => {
			store.commit('base/login', user); //保存本地用户信息到vuex
		})
	},
	/**
	 * 退出登录。跳转到首页。首页在store.state.pages.home.path设置
	 * @param {Object} e
	 */
	logout(e) {
		var that = this;
		store.commit('base/logout');
		uni.removeStorage({
			key: that.USER_KEY
		})
		//util.msg("0000000000")
		util.goTo(store.state.pages.home.path, "reLaunch") //跳转到首页页面		
	},
	submit(url, data, callback) {
		var that = this;
		http.post(url, data).then(res => {
			util.msg(res)
			if (callback) {
				callback(res)
			} else {
				util.msg(res.msg)
			}
		}) //.catch(e => util.msg("签到失败:"+e))			
	},
	/**
	 * 上传图片和视频
	 * @param {Object} param {path:"",data:{},callback:function(res){}}  path必填
	 */
	upload(param) {
		var user = uni.getStorageSync("USER_KEY");
		const uploadTask = uni.uploadFile({
			url: util.baseUrl + '/api/Image/Upload',
			filePath: param.path,
			name: 'file',
			header: { 
				Authorization: 'Bearer ' + (user&&user.token)
			},
			formData: param.data || {},
			success: function(res) {
				//console.log(uploadFileRes.data);
				param.callback && param.callback(res)
			}
		});
		uploadTask.onProgressUpdate(function(res) {
			/* _self.percent = res.progress;
			console.log('上传进度' + res.progress);
			console.log('已经上传的数据长度' + res.totalBytesSent);
			console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend); */
			param.progress && param.progress(res.progress, res.totalBytesSent, res.totalBytesExpectedToSend)
		});
	} 
	/**
	 * 可以放其他的一些常用方法接口
	 */
	/* getClockTeacher() {
		var that = this;
		return http.post("/api/stu/getclockteacher")
	} */
}
